//
//  ClassViewController.m
//  FengyuanXS
//
//  Created by 王冬 on 2020/7/27.
//  Copyright © 2020 王冬. All rights reserved.
//

#import "ClassViewController.h"

@interface ClassViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *listArray;

@end

@implementation ClassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.rowHeight = 50;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"111"];
    
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self getdata];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    
    return self.listArray.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    ClassModel *model = self.listArray[section];
    
    return  model.children.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"111" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor whiteColor];
     ClassModel *model = self.listArray[indexPath.section];
    
    ClassListModel *model1 = model.children[indexPath.row];
    cell.textLabel.text = model1.label;
    
    return  cell;
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ClassModel *model = self.listArray[indexPath.section];
      
      ClassListModel *model1 = model.children[indexPath.row];
    if (self.claasName) {
        self.claasName(model1.label, model1.value,model.value,model.label);
        [self.popupController dismiss];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return  50;
    
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
      ClassModel *model = self.listArray[section];
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 50)];
    headView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.textColor = [UIColor blueColor];
    [headView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.bottom.mas_equalTo(0);
    }];
    titleLabel.text = model.label;
    
    return headView;
    
}

- (IBAction)cancelBtn:(UIButton *)sender {
    
    [self.popupController dismiss];
}

-(void)getdata{
    [[ApiManager ShardInstance]bumenResultBlock:^(NSDictionary *data, NSError *error) {
        BaseModel *model = [BaseModel mj_objectWithKeyValues:data];
        if (model.code == 1) {
            self.listArray = [ClassModel mj_objectArrayWithKeyValuesArray:data[@"rs"]];
            [self.tableView reloadData];
        }
        
        
    }];
    
}

@end
